﻿Public Class oWarehouse
    Public Function GetAllWarehouse(ByVal dataBaseName As String, ByVal companyCode As String, ByVal isAdmin As Boolean) As DataSet
        Dim connect As New Connection()
        Try
            Dim ds As New DataSet("oWarehouse")
            Dim errMsg As String = connect.setSQLDB(dataBaseName)
            If errMsg.Length = 0 Then
                If isAdmin Then
                    ds = connect.ObjectGetAll_Query_SAP("SELECT * FROM oWarehouse T0 JOIN oCompany T1 ON T0.CompanyCode = T1.CompanyCode JOIN oUser T2 ON T0.CreatedUserID = T2.UserID  ORDER BY T0.WarehouseID DESC")
                Else
                    ds = connect.ObjectGetAll_Query_SAP("SELECT * FROM oWarehouse T0 JOIN oCompany T1 ON T0.CompanyCode = T1.CompanyCode JOIN oUser T2 ON T0.CreatedUserID = T2.UserID  WHERE T0.CompanyCode = @Param1  ORDER BY T0.WarehouseID DESC", New Object() {companyCode})

                End If
            End If
            Return ds
        Catch ex As Exception
            Throw ex
        End Try
    End Function
    Public Function GetWarehouseByCode(ByVal dataBaseName As String, ByVal WarehouseCode As String) As DataSet
        Dim connect As New Connection()
        Try
            Dim ds As New DataSet("oWarehouse")
            Dim errMsg As String = connect.setSQLDB(dataBaseName)
            If errMsg.Length = 0 Then
                ds = connect.ObjectGetAll_Query_SAP("SELECT * FROM oWarehouse  T0 JOIN oCompany   T1 ON T0.CompanyCode = T1.CompanyCode  JOIN oUser T2 ON T0.CreatedUserID = T2.UserID WHERE WarehouseCode = @Param1", New Object() {WarehouseCode})
            End If
            Return ds
        Catch ex As Exception
            Throw ex
        End Try
    End Function
    Public Function UpdateJsonWarehouse(
                                        ByVal WarehouseId As String, _
                                       ByVal WarehouseCode As String, ByVal WarehouseName As String, ByVal companyCode As String, ByVal address As String, _
                                       ByVal createdUserId As String, ByVal createdDate As DateTime, ByVal isActive As Boolean, _
                                       ByVal databaseName As String, ByVal isInsert As Boolean) As String
        Dim connect As New Connection()
        Try
            Dim errMsg As String = connect.setSQLDB(databaseName)
            If errMsg.Length = 0 Then
                Dim params = New Object() {WarehouseCode, WarehouseName, companyCode, address, createdUserId}
                Dim query As New StringBuilder
                If isInsert = True Then
                    query.Append(" INSERT INTO [oWarehouse]")
                    query.Append("  ([WarehouseCode]")
                    query.Append(" ,[WarehouseName]")
                    query.Append(" ,[CompanyCode]")
                    query.Append(" ,[Address]")
                    query.Append(" ,[CreatedUserID])")
                    query.Append(" VALUES (@Param1,@Param2,@Param3,@Param4,@Param5)")
                    Dim count As Integer = connect.Object_Execute_SAP(query.ToString(), params)
                Else
                    query.Append("UPDATE  [oWarehouse] SET ")
                    query.Append("  [WarehouseName] = @Param1")
                    query.Append("  ,[WarehouseCode] = @Param2")
                    query.Append("  ,[Address] = @Param3")
                    query.Append(" WHERE [WarehouseID] = @Param4")
                    connect.Object_Execute_SAP(query.ToString(), New Object() {WarehouseName, WarehouseCode, address, WarehouseId})
                End If
            Else
                Return errMsg
            End If
        Catch ex As Exception

            Return ex.Message
        End Try
        Return String.Empty
    End Function
    Public Function DeleteJsonWarehouse(
                                       ByVal WarehouseId As String, _
                                      ByVal databaseName As String) As String
        Dim connect As New Connection()
        Try
            Dim errMsg As String = connect.setSQLDB(databaseName)
            If errMsg.Length = 0 Then
                Dim query As New StringBuilder
                query.Append("DELETE  [oWarehouse]  ")
                query.Append(" WHERE [WarehouseID] = @Param1")
                connect.Object_Execute_SAP(query.ToString(), New Object() {WarehouseId})
            Else
                Return errMsg
            End If
        Catch ex As Exception

            Return ex.Message
        End Try
        Return String.Empty
    End Function
End Class
